<?php

/**
 * PX form. Custom Form for Transnational
 *
 * @package    form
 * @subpackage Person
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class PxForm extends BasePersonForm
{
  public function configure()
  {
	
		unset(
      $this['created_at'], $this['updated_at'],$this['visible']//, $this['type']
    );
    $this->widgetSchema['type'] = new sfWidgetFormInputHidden();
    $this->widgetSchema['author'] = new sfWidgetFormInputHidden();
    $this->widgetSchema['client_id'] = new sfWidgetFormInputHidden();
	//	$this->widgetSchema['px_no'] = new sfWidgetFormInput(array(),array('readonly'=>'readonly'));
		$phonesForm = new sfForm();
		if (!$this->isNew()) {  
	   // embed all old Phone forms
	   foreach ($this->getObject()->getPhone() as $phone) {
	   	$phonesForm->embedForm('phone'.$phone->getId(), new PhoneForm($phone));
	   }
		}
		//create blank phone form at end
		 $phonesForm->embedForm('phone', new PhoneForm());
		 $this->embedForm('phones', $phonesForm);








		$websitesForm = new sfForm();
		 if (!$this->isNew()) {  
	    // embed all Website forms
	    foreach ($this->getObject()->getWebsites() as $website) {
	    $websitesForm->embedForm('website'.$website->getId(), new WebsitesForm($website));

	    // set a custom label for the embedded form
	   // $this->widgetSchema['websites'.$websites->getId()]->setLabel('Websites: '.$websites->getName());

	    // change the name widget to sfWidgetFormInputDelete
	   // $this->widgetSchema['subcategory'.$subcategory->getId()]['name'] = new sfWidgetFormInputDelete(array(
	   // 'url' => 'category/deleteSubcategory',      // required
	   // 'model_id' => $subcategory->getId(),        // required
	   // 'confirm' => 'Sure???',                     // optional
	   // ));		   
		   }
		}
	//create blank website form
	  $websitesForm->embedForm('website', new WebsitesForm());
	  $this->embedForm('websites', $websitesForm);




			$addresssForm = new sfForm();
			if (!$this->isNew()) {  
		   // embed all old address forms
		   foreach ($this->getObject()->getAddress() as $address) {
		   	$addresssForm->embedForm('address'.$address->getId(), new AddressForm($address));
		   }
			}
			//create blank address form at end
			 $addresssForm->embedForm('address', new AddressForm());
			 $this->embedForm('addresss', $addresssForm);






			$emailsForm = new sfForm();
			if (!$this->isNew()) {  
		   // embed all old email forms
		   foreach ($this->getObject()->getEmail() as $email) {
		   	$emailsForm->embedForm('email'.$email->getId(), new EmailForm($email));
		   }
			}
			//create blank email form at end
			 $emailsForm->embedForm('email', new EmailForm());
			 $this->embedForm('emails', $emailsForm);





				$smasForm = new sfForm();
				if (!$this->isNew()) {  
			   // embed all old SMA forms
			   foreach ($this->getObject()->getSocialMediaAccount() as $sma) {
			   	$smasForm->embedForm('socialmediaaccount'.$sma->getId(), new SocialMediaAccountForm($sma));
			   }
				}
				//create blank SMA form at end
				 $smasForm->embedForm('socialmediaaccount', new SocialMediaAccountForm());
				 $this->embedForm('socialmediaaccounts', $smasForm);








				








				$this->validatorSchema->setOption('allow_extra_fields', true);
	
  }



 /**
   * Update the Person_id of all the forms after the booking has been saved
   */
  public function saveEmbeddedForms($con = null, $forms = null)
  {    
    foreach($this->embeddedForms['phones']->getEmbeddedForms() as $phonesForms)
    {
      if (!$phonesForms->getObject()->getPersonId())
      {
        $phonesForms->getObject()->setPersonId($this->getObject()->getId());
				//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
      }
    }

		foreach($this->embeddedForms['websites']->getEmbeddedForms() as $websitesForms)
    {
      if (!$websitesForms->getObject()->getPersonId())
      {
        $websitesForms->getObject()->setPersonId($this->getObject()->getId());
				//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
      }
    }

		foreach($this->embeddedForms['emails']->getEmbeddedForms() as $emailsForms)
    {
      if (!$emailsForms->getObject()->getPersonId())
      {
        $emailsForms->getObject()->setPersonId($this->getObject()->getId());
				//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
      }
    }

		foreach($this->embeddedForms['socialmediaaccounts']->getEmbeddedForms() as $smaForms)
    {
      if (!$smaForms->getObject()->getPersonId())
      {
        $smaForms->getObject()->setPersonId($this->getObject()->getId());
				//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
      }
    }

		foreach($this->embeddedForms['addresss']->getEmbeddedForms() as $addressForms)
    {
      if (!$addressForms->getObject()->getPersonId())
      {
        $addressForms->getObject()->setPersonId($this->getObject()->getId());
				//$allForms->getObject()->setAuthor($this->getObject()->getAuthor()); setting of author. implement when already using sfGuard
      }
    }
    parent::saveEmbeddedForms($con, $forms);
  }


	public function bind(array $taintedValues = null, array $taintedFiles = null)
	{
		
		
		// add all the new fields that don't exist (because they were added dynamically)
    foreach($taintedValues['phones'] as $key=>$newTodo)
    {
      if (!isset($this['phones'][$key]))
      {
       
			  if (is_null($taintedValues['phones'][$key]['number'])|| strlen($taintedValues['phones'][$key]['number']) === 0 )
			  {

			  }
				else
				{
					$this->addNewPhone($key);
				}

      }
    }

		// add all the new fields that don't exist (because they were added dynamically)
    foreach($taintedValues['addresss'] as $key=>$newTodo)
    {
      if (!isset($this['addresss'][$key]))
      {
       
			  if (is_null($taintedValues['addresss'][$key]['unit_no'])|| strlen($taintedValues['addresss'][$key]['unit_no']) === 0 )
			  {

			  }
				else
				{
					$this->addNewAddress($key);
				}

      }
    }
		
		
		
		
		
		
		
		
		
		
		
		
		
		 if (is_null($taintedValues['addresss']['address']['unit_no']) || strlen($taintedValues['addresss']['address']['unit_no']) === 0 ) 
		  {
		    unset($this->embeddedForms['addresss']['address']);
		    $this->validatorSchema['addresss']['address'] = new sfValidatorPass();
		    $taintedValues['addresss']['address'] = null;
		  }

		  if (is_null($taintedValues['websites']['website']['url'])|| strlen($taintedValues['websites']['website']['url']) === 0 )
		  {
		    unset($this->embeddedForms['websites']['website']);
		    $this->validatorSchema['websites']['website'] = new sfValidatorPass();
		    $taintedValues['websites']['website'] = null;
		  }

			if (is_null($taintedValues['emails']['email']['email'])|| strlen($taintedValues['emails']['email']['email']) === 0 )
		  {
		    unset($this->embeddedForms['emails']['email']);
		    $this->validatorSchema['emails']['email'] = new sfValidatorPass();
		    $taintedValues['emails']['email'] = null;
		  }

		  if (is_null($taintedValues['socialmediaaccounts']['socialmediaaccount']['account_name'])|| strlen($taintedValues['socialmediaaccounts']['socialmediaaccount']['account_name']) === 0 )
		  {
		    unset($this->embeddedForms['socialmediaaccounts']['socialmediaaccount']);
		    $this->validatorSchema['socialmediaaccounts']['socialmediaaccount'] = new sfValidatorPass();
		    $taintedValues['socialmediaaccounts']['socialmediaaccount'] = null;
		  }

			if (is_null($taintedValues['phones']['phone']['number'])|| strlen($taintedValues['phones']['phone']['number']) === 0 )
		  {
		    unset($this->embeddedForms['phones']['phone']);
		    $this->validatorSchema['phones']['phone'] = new sfValidatorPass();
		    $taintedValues['phones']['phone'] = null;
		  }

		


		  
		



		  parent::bind($taintedValues, $taintedFiles);
	}
	
	
	protected function doSave($con = null)
  {
    if (is_null($con))
    {
      $con = $this->getConnection();
    }
		$this->object->setType('PX');
    $this->updateObject();

    $this->object->save($con);

    // embedded forms
    $this->saveEmbeddedForms($con);
	//	$this->saveEmbeddedFormsNested($con);
  }

  public function saveEmbeddedFormsNested($con = null, $forms = null)
  {
		foreach($this->embeddedForms['contactpersons']->getEmbeddedForms() as $contactpersonForms)
		{
			$contactpersonForms->embeddedForms['contact']->getObject()->setPersonId($contactpersonForms->getObject()->getId());

			foreach($contactpersonForms->embeddedForms['phones']->getEmbeddedForms() as $phonesForms)
	    {
	      if (!$phonesForms->getObject()->getPersonId())
	      {
	        $phonesForms->getObject()->setPersonId($contactpersonForms->getObject()->getId());
	      }
	    }
		
		

			foreach($contactpersonForms->embeddedForms['emails']->getEmbeddedForms() as $emailsForms)
	    {
	      if (!$emailsForms->getObject()->getPersonId())
	      {
	        $emailsForms->getObject()->setPersonId($contactpersonForms->getObject()->getId());
				
	      }
	    }

			foreach($contactpersonForms->embeddedForms['socialmediaaccounts']->getEmbeddedForms() as $smaForms)
	    {
	      if (!$smaForms->getObject()->getPersonId())
	      {
	        $smaForms->getObject()->setPersonId($contactpersonForms->getObject()->getId());
				
	      }
	    }
		}
	    parent::saveEmbeddedForms($con, $forms);
	}

	public function addNewPhone($name)
  {
    $this->embeddedForms['phones']->embedForm($name, new PhoneForm());
    $this->embedForm('phones', $this->embeddedForms['phones']); // re-embed the form

  }

	public function addNewAddress($name)
  {
    $this->embeddedForms['addresss']->embedForm($name, new AddressForm());
    $this->embedForm('addresss', $this->embeddedForms['addresss']); // re-embed the form

  }

}